package com.kaiyiweitong.tongxing.fragment.coupon_fragment;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

import com.kaiyiweitong.tongxing.R;
import com.kaiyiweitong.tongxing.activity.MainActivity;
import com.kaiyiweitong.tongxing.util.CommonUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/12/21 0021.
 */

public class GetAwardFragment extends Fragment {
    @Bind(R.id.pic)
    ImageView mPic;
    @Bind(R.id.start)
    ImageView mStart;
    private View mView;
    private MainActivity mActivity;

    private int count = 8;//格子数
    private int circle = 12;//每次圈数
    private long time = 5000;//旋转总时间

    private int totalCircle = 0;//总圈数
    private List<String> mList;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_get_award, null);
        mActivity = (MainActivity) getActivity();
        ButterKnife.bind(this, mView);
        init();
        return mView;
    }

    private void init() {
        mList = new ArrayList<>();
        mList.add("注册公司");
        mList.add("法律顾问");
        mList.add("商标代理");
        mList.add("注册公司");
        mList.add("注册公司");
        mList.add("注册公司");
        mList.add("代理记账");
        mList.add("人事社保");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick(R.id.start)
    public void onViewClicked() {
        getAward();
    }


    private void getAward() {
        if (totalCircle != 0) {
            //还原
            ObjectAnimator rotation = ObjectAnimator.ofFloat(mPic, "rotation", 0);
            rotation.setDuration(0);
            rotation.start();
        }
        final int random = new Random().nextInt(count);
        final ObjectAnimator rotation = ObjectAnimator.ofFloat(mPic, "rotation", circle * 360 + 360 / count * random);
        rotation.setDuration(time);
        rotation.setInterpolator(new LinearInterpolator());
        rotation.start();

        rotation.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                mStart.setClickable(false);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                totalCircle += circle;
                CommonUtil.toast(mActivity, mList.get(random));
                mStart.setClickable(true);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
    }
}
